/**
 * 
 */
package com.punegtug.opensocial.gifts.servlet;

import java.util.List;
import java.util.StringTokenizer;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import com.punegtug.opensocial.gifts.model.Gift;
import com.punegtug.opensocial.gifts.model.GiftTransaction;
import com.punegtug.opensocial.gifts.persistence.PMF;

/**
 * @author rohitghatol
 * 
 */
public class InitServlet extends HttpServlet {

	public static final String GIFTS = "gifts";

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig)
	 */
	@Override
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		super.init(config);
		PersistenceManager pMgr = PMF.get().getPersistenceManager();
		List<Gift> gifts = (List<Gift>) pMgr.newQuery(Gift.class).execute();
		pMgr.deletePersistentAll(gifts);

		List<GiftTransaction> giftsTxns = (List<GiftTransaction>) pMgr
				.newQuery(GiftTransaction.class).execute();
		pMgr.deletePersistentAll(giftsTxns);

		String giftsString=config.getInitParameter(GIFTS);
		StringTokenizer tokenizer=new StringTokenizer(giftsString,",");
		while(tokenizer.hasMoreElements()){
			String giftName=tokenizer.nextToken().trim();
			Gift gift=new Gift(giftName);
			pMgr.makePersistent(gift);
		}

	}

}
